package com.thoughtworks.opensource.yui.dd.client.windowresources;
/*
   Copyright 2006 C. K. Sidu Ponnappa

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Created by IntelliJ IDEA.
 * Author: C. K. Sidu Ponnappa
 * Date: Nov 21, 2006
 * Time: 5:27:30 PM
 * ThoughtWorks India Pvt. Ltd.
 */
// understands the concept of an isolated window with its own resources.
public abstract class DisplayAreaViewBase implements DragDropListener {
    protected RootPanel rootPanel;
    protected Draggables draggables;
    protected Droppables droppableAreas;
    protected ElementToWidgetMap elementToWidgetMap;
    protected WidgetToDomainObjectMap widgetToObjectMap;
    protected DisplayAreaController controller;

    protected DisplayAreaViewBase() {
        controller = new DisplayAreaController();
        draggables = new Draggables(this);
        droppableAreas = new Droppables();
        elementToWidgetMap = new ElementToWidgetMap();
    }

    protected DisplayAreaViewBase(RootPanel rootPanel) {
        this();
        this.rootPanel = rootPanel;
    }

    public void addWidgetToPanelWithMapping(Widget w, Panel p) {
        p.add(w);
        elementToWidgetMap.put(w);
    }

    public void addToDraggables(Widget w) {
        draggables.add(w);
    }

    public void onDragDrop(Element draggedElement, Element dropAreaElement) {

        Widget draggedWidget = draggables.getWidgetFor(draggedElement);
        DropArea dropAreaWidget = droppableAreas.getDropAreaFor(dropAreaElement);
//        logToElementsToConsole(draggedElement, draggedWidget, dropAreaElement, dropAreaWidget);
//        dropAreaWidget.dragDropNotification(widgetToObjectMap.get(draggedWidget));
        dropAreaWidget.dragDropNotification(draggedWidget);
    }

    public void onDragEnter(Element draggedElement, Element dropAreaElement) {
        Widget draggedWidget = draggables.getWidgetFor(draggedElement);
        DropArea dropAreaWidget = droppableAreas.getDropAreaFor(dropAreaElement);
//            logToElementsToConsole(draggedElement, draggedWidget, dropAreaElement, dropAreaWidget);
        dropAreaWidget.dragEnterNotification(widgetToObjectMap.get(draggedWidget));
    }
/*
    private void logToElementsToConsole(Element dragged, Widget draggedWidget, Element dropArea, DropArea dropAreaWidget) {
        GWT.log("-------------------------", null);
        GWT.log(dragged.toString() + ":" + dragged.hashCode(), null);
        GWT.log(draggedWidget.toString() + ":" + draggedWidget.hashCode(), null);
        GWT.log(dropArea.toString() + ":" + dropArea.hashCode(), null);
        GWT.log(dropAreaWidget.toString() + ":" + dropAreaWidget.hashCode(), null);
        GWT.log("-------------------------", null);
    }
*/

    public void mapWidgetToModel(Widget widget, Object domainObject) {
        widgetToObjectMap.put(widget, domainObject);
    }

    public Object getModelForWidget(Widget widget) {
        return widgetToObjectMap.get(widget);
    }
}
